// #sijapp cond.if protocols_ICQ is "true" #
// #sijapp cond.if modules_SERVERLISTS is "true" #
package protocol.icq.action;

import java.util.*;
import java.io.*;
import jimm.cl.*;
import jimm.comm.*;
import jimm.*;
import protocol.icq.*;
import protocol.icq.IcqContact;
import protocol.icq.packet.*;

public class ServerListsAction extends IcqAction {

	// Receive timeout
	private static final int TIMEOUT = 3 * 1000; // milliseconds
	
    public static final int VISIBLE_LIST   = 0x0002;
    public static final int INVISIBLE_LIST = 0x0003;
    public static final int IGNORE_LIST    = 0x000E;

    public static final int ADD_INTO_LIST      = 0;
    public static final int REMOVE_FROM_LIST   = 1;

	/****************************************************************************/
	
	// Date of init

    private int subaction;
    private int list;
    private IcqContact item;
    private long lastActivity;
    private int id;

	// Constructor
	public ServerListsAction(int list, IcqContact item) {
		super();
        this.list      = list;
        this.item      = item;
	}

    // Init action
	public void init() throws JimmException {
        id = 0;
        switch (list) {
            case VISIBLE_LIST:
                id = getIcq().getVisibleId(item);
                break;
                
            case INVISIBLE_LIST:
                id = getIcq().getInvisibleId(item);
                break;
                
            case IGNORE_LIST:
                id = getIcq().getIgnoreId(item);
                break;
            
        }

        if (id == 0) {
            id = getIcq().createRandomId();
            subaction = ADD_INTO_LIST;
        } else {
            subaction = REMOVE_FROM_LIST;
        }
        		
        Util stream = new Util();
        
        stream.writeLenAndUtf8String(item.getUserId());
        stream.writeWordBE(0);
        stream.writeWordBE(id);
        stream.writeWordBE(list);
		stream.writeWordLE(0);

        SnacPacket packet = null;
        switch (subaction) {
            case ADD_INTO_LIST:
                packet = new SnacPacket(SnacPacket.SSI_FAMILY, SnacPacket.CLI_ROSTERADD_COMMAND, stream.toByteArray());
                break;
                
            case REMOVE_FROM_LIST:
                packet = new SnacPacket(SnacPacket.SSI_FAMILY, SnacPacket.CLI_ROSTERDELETE_COMMAND, stream.toByteArray());
                id = 0;
                break;
        }
        sendPacket(packet);
        lastActivity = System.currentTimeMillis();
	}
	
    private int packetCounter = 0;
    
	// Forwards received packet, returns true if packet was consumed
	public boolean forward(Packet packet) throws JimmException {

        // Watch out for SRV_FROMICQSRV packet
		if (packet instanceof SnacPacket) {
            SnacPacket snacPacket = (SnacPacket)packet;
            if (snacPacket.getFamily() != SnacPacket.SSI_FAMILY
                    || snacPacket.getCommand() != SnacPacket.SRV_UPDATEACK_COMMAND) {
                return false;
            }

            byte[] data = snacPacket.getData();
            int result = Util.getWordLE(data, 0);
            if (result == 0) {
                switch (list) {
                    case VISIBLE_LIST:
                        getIcq().setVisibleId(item, id);
                        break;

                    case INVISIBLE_LIST:
                        getIcq().setInvisibleId(item, id);
                        break;

                    case IGNORE_LIST:
                        getIcq().setIgnoreId(item, id);
                        break;
                }
                getIcq().ui_updateContact(item);
            }
            packetCounter++;
            return true;
		}

		return false;
	}
	
	// Returns true if the action is completed
	public boolean isCompleted() {
		return (packetCounter >= 1);
	}

	// Returns true if an error has occured
	public boolean isError() {
		return (lastActivity + TIMEOUT) < System.currentTimeMillis();
	}
}
// #sijapp cond.end #
// #sijapp cond.end #
